/*
 * @Author: szx
 * @Date: 2022-07-04 13:01:25
 * @LastEditTime: 2022-07-04 13:30:12
 * @Description:
 * @FilePath: \leetcode\1-99\31\31.js
 */
/**
 * @param {number[]} nums
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var nextPermutation = function (nums) {
    //从后往前遍历找到相邻升序的数字
    let i = nums.length - 2;
    while (i >= 0 && nums[i] >= nums[i + 1]) i--;
    //从i到末尾找到比i大的数字交换
    let j = nums.length - 1;
    if (i >= 0) {
        while (nums[j] <= nums[i]) j--;
        [nums[i], nums[j]] = [nums[j], nums[i]];
    }
    i++;
    j = nums.length - 1;
    // 然后reserve，i+1到末尾
    while (i < j) {
        [nums[i], nums[j]] = [nums[j], nums[i]];
        i++;
        j--;
    }
    return nums;
};
